# Dependencies and Setup
import hvplot.pandas
import pandas as pd
import requests
import numpy as np
import json
# https://pypi.org/project/geoviews/: conda install -c pyviz geoviews-core
# pip install pyproj
import cartopy.crs as ccrs
import geoviews as gv
# Import API key
from api_keys import geoapify_key
# Load the CSV file created in Part 1 into a Pandas DataFrame
city_data_df = pd.read_csv("output_data/cities.csv")
# city_data_df = pd.read_csv("output_data/clean_city_data.csv")
# Display sample data
city_data_df.head()
| City_ID | City | Lat | Lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | east london | -33.0153 | 27.9116 | 67.14 | 63.0 | 75.0 | 4.61 | ZA | 1.675011e+09 |
| 1 | 1 | cherskiy | 68.7500 | 161.3000 | -14.40 | 98.0 | 77.0 | 1.77 | RU | 1.675011e+09 |
| 2 | 2 | kodiak | 57.7900 | -152.4072 | 35.11 | 100.0 | 100.0 | 0.00 | US | 1.675011e+09 |
| 3 | 3 | santa cruz | -17.8000 | -63.1667 | 82.45 | 74.0 | 75.0 | 14.97 | BO | 1.675011e+09 |
| 4 | 4 | rikitea | -23.1203 | -134.9692 | 78.55 | 74.0 | 77.0 | 10.87 | PF | 1.675011e+09 |
city_data_df DataFrame. The size of the point should be the humidity in each city.¶%%capture --no-display
# Configure the map plot
map_plot_3 = city_data_df.hvplot.points(
'Lng',
'Lat',
geo = True,
tiles = "OSM",
frame_width = 700,
frame_height = 500,
size = "Humidity",
scale = 1,
color = "City"
)
# Display the map
map_plot_3
city_data_df DataFrame to find your ideal weather condition¶# Narrow down cities that fit criteria and drop any results with null values
# Drop any rows with null values
clean_cities_df = city_data_df.dropna(how="any")
# print(clean_cities_df.count())
clean_cities_df = clean_cities_df[
(clean_cities_df['Max Temp'] > 18) & (clean_cities_df['Max Temp'] < 25) &
(clean_cities_df['Wind Speed'] < 5)
]
# Display sample data
clean_cities_df
| City_ID | City | Lat | Lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | |
|---|---|---|---|---|---|---|---|---|---|---|
| 318 | 351 | port hardy | 50.6996 | -127.4199 | 24.98 | 93.0 | 20.0 | 4.61 | CA | 1.675011e+09 |
| 362 | 397 | shitanjing | 39.2342 | 106.3439 | 23.00 | 24.0 | 0.0 | 2.84 | CN | 1.675011e+09 |
| 363 | 398 | zhaoyuan | 37.3592 | 120.3964 | 24.85 | 70.0 | 2.0 | 3.31 | CN | 1.675011e+09 |
| 486 | 534 | yumen | 40.2833 | 97.2000 | 21.79 | 26.0 | 86.0 | 4.56 | CN | 1.675011e+09 |
| 511 | 561 | kars | 40.6020 | 43.0950 | 21.65 | 93.0 | 100.0 | 0.00 | TR | 1.675011e+09 |
| 548 | 598 | hami | 42.8000 | 93.4500 | 20.25 | 32.0 | 97.0 | 2.98 | CN | 1.675011e+09 |
hotel_df.¶# Use the Pandas copy function to create DataFrame called hotel_df to store the city, country, coordinates, and humidity
hotel_df = clean_cities_df[["City","Country","Lat","Lng","Humidity"]].copy()
# Add an empty column, "Hotel Name," to the DataFrame so you can store the hotel found using the Geoapify API
hotel_df['Hotel Name'] = np.nan
# Display sample data
hotel_df
| City | Country | Lat | Lng | Humidity | Hotel Name | |
|---|---|---|---|---|---|---|
| 318 | port hardy | CA | 50.6996 | -127.4199 | 93.0 | NaN |
| 362 | shitanjing | CN | 39.2342 | 106.3439 | 24.0 | NaN |
| 363 | zhaoyuan | CN | 37.3592 | 120.3964 | 70.0 | NaN |
| 486 | yumen | CN | 40.2833 | 97.2000 | 26.0 | NaN |
| 511 | kars | TR | 40.6020 | 43.0950 | 93.0 | NaN |
| 548 | hami | CN | 42.8000 | 93.4500 | 32.0 | NaN |
# Set parameters to search for a hotel
radius = 10 * 1000
## I don't know something wrong, geoapify_key cannot be obtained from api_keys.py file
# params = {
# "radius": radius,
# "type": "accommodation.hotel",
# "apiKey": geoapify_key
# }
# Therefore, I put geoapify_key's value here
params = {
"radius": radius,
"type": "accommodation.hotel",
"apiKey": "aa46f49c727b4be1aeb53b12259f3e6a"
}
# Print a message to follow up the hotel search
print("*********** Starting hotel search ***********")
# Iterate through the hotel_df DataFrame
for index, row in hotel_df.iterrows():
# get latitude, longitude from the DataFrame
lat = row['Lat']
lng = row['Lng']
# Add filter and bias parameters with the current city's latitude and longitude to the params dictionary
params["filter"] = f"circle:{lng},{lat},{radius}"
params["bias"] = f"proximity:{lng},{lat}"
# Set base URL
base_url = "https://api.geoapify.com/v2/places"
# Make and API request using the params dictionaty
name_address = requests.get(base_url, params=params)
# Convert the API response to JSON format
name_address = name_address.json()
# Grab the first hotel from the results and store the name in the hotel_df DataFrame
try:
hotel_df.loc[index, "Hotel Name"] = name_address["features"][0]["properties"]["address_line1"]
# print(json.dumps(name_address, indent=4, sort_keys=True))
except (KeyError, IndexError):
# If no hotel is found, set the hotel name as "No hotel found".
hotel_df.loc[index, "Hotel Name"] = "No hotel found"
# Log the search results
print(f"{hotel_df.loc[index, 'City']} - nearest hotel: {hotel_df.loc[index, 'Hotel Name']}")
print("*********** Complete searching hotel ***********")
# Display sample data
hotel_df
*********** Starting hotel search *********** port hardy - nearest hotel: Quarterdeck Inn shitanjing - nearest hotel: No hotel found zhaoyuan - nearest hotel: No hotel found yumen - nearest hotel: No hotel found kars - nearest hotel: Grand Ani Hotel hami - nearest hotel: Hami Hotel 2nd Bldg *********** Complete searching hotel ***********
| City | Country | Lat | Lng | Humidity | Hotel Name | |
|---|---|---|---|---|---|---|
| 318 | port hardy | CA | 50.6996 | -127.4199 | 93.0 | Quarterdeck Inn |
| 362 | shitanjing | CN | 39.2342 | 106.3439 | 24.0 | No hotel found |
| 363 | zhaoyuan | CN | 37.3592 | 120.3964 | 70.0 | No hotel found |
| 486 | yumen | CN | 40.2833 | 97.2000 | 26.0 | No hotel found |
| 511 | kars | TR | 40.6020 | 43.0950 | 93.0 | Grand Ani Hotel |
| 548 | hami | CN | 42.8000 | 93.4500 | 32.0 | Hami Hotel 2nd Bldg |
%%capture --no-display
# Configure the map plot
map_plot_3 = hotel_df.hvplot.points(
'Lng',
'Lat',
geo = True,
tiles = "OSM",
frame_width = 700,
frame_height = 500,
scale = 1,
color = "City",
hover_cols = ["Hotel Name", "Country"]
)
# Display the map
map_plot_3